package org.lzy.shop.viewmodel

import AddressRepositoryImpl
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.lzy.shop.base.BaseViewModel
import org.lzy.shop.datas.AddAddressRequest
import org.lzy.shop.datas.DelAddress
import org.lzy.shop.datas.EditAddressRequest
import org.lzy.shop.response.AddressResponse
import org.lzy.shop.viewmodel.repository.AddressRepository

/**
 * 地址的VM层
 */
class AddressModel: BaseViewModel() {

    private val addressRepository: AddressRepository = AddressRepositoryImpl()
    // 地址列表数据
    val addressListData = MutableLiveData<MutableList<AddressResponse>?>()

    // 地址操作结果数据
    val addressOperationData = MutableLiveData<String?>()


    /**
     * 获取地址列表
     */
    fun getAddressList() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = addressRepository.getAddressList()
                if (result != null) {
                    addressListData.value = result
                } else {
                    errorMessage.value = "获取地址列表失败，请重试"
                    addressListData.value = null
                }
            } catch (e: Exception) {
                e.printStackTrace()
                errorMessage.value = e.message ?: "获取地址列表失败，请重试"
                addressListData.value = null
            }
        }
    }

    /**
     * 添加地址
     */
    fun addAddress(request: AddAddressRequest) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = addressRepository.addAddress(request)
                if (result != null) {
                    addressOperationData.value = result
                } else {
                    errorMessage.value = "添加地址失败，请重试"
                    addressOperationData.value = null
                }
            } catch (e: Exception) {
                e.printStackTrace()
                errorMessage.value = e.message ?: "添加地址失败，请重试"
                addressOperationData.value = null
            }
        }
    }

    /**
     * 更新地址
     */
    fun updateAddress(request: EditAddressRequest) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = addressRepository.updateAddress(request)
                if (result != null) {
                    addressOperationData.value = result
                } else {
                    errorMessage.value = "更新地址失败，请重试"
                    addressOperationData.value = null
                }
                addressOperationData.value = result
            } catch (e: Exception) {
                e.printStackTrace()
                errorMessage.value = e.message ?: "更新地址失败，请重试"
                addressOperationData.value = null
            }
        }
    }

    /**
     * 删除地址
     */
    fun delAddress(request: DelAddress) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val result = addressRepository.delAddress(request)
                if (result != null) {
                    addressOperationData.value = result
                } else {
                    errorMessage.value = "删除地址失败，请重试"
                    addressOperationData.value = null
                }
                addressOperationData.value = result
            } catch (e: Exception) {
                e.printStackTrace()
                errorMessage.value = e.message ?: "删除地址失败，请重试"
                addressOperationData.value = null
            }
        }
    }
}